package com.geek.pokermanager.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.geek.pokermanager.model.PokerGroup;
import com.geek.pokermanager.model.SerializableData;
import com.geek.pokermanager.service.PokerGrupService;
import com.geek.pokermanager.util.UUIDTool;

import ydh.cicada.dao.JdbcDao;
import ydh.cicada.query.QueryObject;

@Service
@Transactional
public class PokerGroupServiceImpl implements PokerGrupService {
	
	@Autowired
	private JdbcDao dao;
	
	@Override
	public int save(PokerGroup group) {
		dao.insert(group);
		return 1;
	}

	@Override
	public List<PokerGroup> getGroup(String pokeStoreId) {
		List<PokerGroup> p=QueryObject.select("poker_group_id pokerGroupId,poker_group_name pokerGroupName,"
											+ " poker_group_no pokerGroupNo,poker_group_desc pokerGroupDesc,"
											+ " pokerData pokerCovertData,poker_store_id pokerStoreId",PokerGroup.class)
				.cond("poker_store_id").equ(pokeStoreId)
				.list(dao);
//		String sql="select pokerData from poker_group where poker_group_id=?";
//		for (PokerGroup pokerGroup : p) {
//			Object bytes=dao.getJdbcTemplate().queryForObject(sql,Object.class, new Object[]{pokerGroup.getPokerGroupId()});
//			try {
//				pokerGroup.setPokerDataByte((byte[])bytes);
//			} catch (ClassNotFoundException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
		return p;
	}

	@Override
	public int update(PokerGroup group) {
		dao.update(group);
		return 1;
	}

	@Override
	public int delete(String id) {
		PokerGroup p=new PokerGroup();
		p.setPokerGroupId(id);
		dao.delete(p);
		return 1;
	}

	@Override
	public int saveSerializableData(List<PokerGroup> group,String roomId) {
		if(group!=null && StringUtils.isNotBlank(roomId)){
			for (PokerGroup pokerGroup : group) {
				pokerGroup=QueryObject.select("poker_group_id pokerGroupId,poker_group_name pokerGroupName,"
						+ " poker_group_no pokerGroupNo,poker_group_desc pokerGroupDesc,"
						+ " pokerData pokerCovertData,poker_store_id pokerStoreId",PokerGroup.class)
						.cond("poker_group_id").equ(pokerGroup.getPokerGroupId())
						.firstResult(dao);
			}
			SerializableData newData=QueryObject.select("serializable_data_id serializableDataId,room_id roomId",SerializableData.class).cond("room_id").equ(roomId).firstResult(dao);
			if(newData==null){
				newData=new SerializableData();
				newData.setRoomId(roomId);
				newData.setSerializableDataId(UUIDTool.getUUID());
				newData.setGroup(group);
				dao.insert(newData);
			}else{
				newData.setGroup(group);
				dao.update(newData);
			}
		}
		return 1;
	}
	
	public SerializableData getSerializbleData(String roomId){
		SerializableData data=QueryObject.select("serializable_data_id serializableDataId,room_id roomId,group groupData",SerializableData.class)
					.cond("room_id").equ(roomId)
					.firstResult(dao);
		return data;
	}

	@Override
	public PokerGroup getGroupData(String groupId) {
		return QueryObject.select("poker_group_id pokerGroupId,poker_group_name pokerGroupName,"
				+ " poker_group_no pokerGroupNo,poker_group_desc pokerGroupDesc,"
				+ " pokerData pokerCovertData,poker_store_id pokerStoreId",PokerGroup.class)
				.cond("poker_group_id").equ(groupId)
				.firstResult(dao);
	}
}
